﻿using Business.Base;
using Business.Base.DB;
using Business.Base.DB.Query;
using Business.Base.Enums;
using Business.Base.Model;
using Configuration;
using PLC.Base;
using PLC.Manager;
using PumpAlgorithm;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.Base.Service.impl
{
    class ExperimentDBService:IExperimentDBService
    {
        MysqlDBManager experimentDBManager;
        MysqlDBManager experimentRecordDBManager =null;


        public void init(Type entityType, Type entityRecordType)
        {
            this.experimentDBManager = MysqlDBManager.dbManagerDic[entityType.Name];
            this.experimentRecordDBManager = MysqlDBManager.dbManagerDic[entityRecordType.Name];
            //this.experimentExcelManager = excel;
        }



        /// <summary>
        /// 查询试验记录
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<Object> QueryExperiment(BaseQuery query)
        {
            return experimentDBManager.Query(query);
        }


        public List<Object> QueryExperimentReocrd(BaseQuery query)
        {
            return experimentRecordDBManager.Query(query);
        }



        /// <summary>
        /// 获取符合条件的记录数量
        /// </summary>
        /// <param name="query">查询条件对象</param>
        /// <returns>查询结果</returns>
        public int GetExperimentCount(BaseQuery query)
        {
            return experimentDBManager.GetCount(query);
        }

        public int GetExperimentRecordCount(BaseQuery query)
        {
            return experimentRecordDBManager.GetCount(query);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="experiment"></param>
        public void DeleteExperiment(Experiment experiment)
        {
            int id = experiment.Id;     

            experimentDBManager.Delete(id);
            //this.DeleteAttachInfo(experiment.ExperimentCode);
            experimentRecordDBManager.DeleteByCondtion("experimentCode='" + experiment.ExperimentCode + "'");


        }

        /// <summary>
        /// 保存对象
        /// </summary>
        /// <param name="experiment"></param>
        public void SaveExperiment(Experiment experiment)
        {
            experimentDBManager.Insert(experiment);
        }

        public void UpdateExperiment(Experiment experiment)
        {
            experimentDBManager.Update(experiment.Id,experiment);
        }

        /// <summary>
        /// 根据id批量删除记录
        /// </summary>
        /// <param name="id"></param>
        public void RemoveExperimentRecordByIds(int[] id)
        {
            if (id == null || id.Length == 0)
            {
                return;
            }

            StringBuilder condition = new StringBuilder();
            condition.Append(" Id in (");
            for (int i = 0; i < id.Length - 1; i++)
            {
                condition.Append(id[i].ToString() + ",");
            }
            condition.Append(id[id.Length - 1].ToString() + ")");


            this.experimentRecordDBManager.DeleteByCondtion(condition.ToString());

        }

        /// <summary>
        /// 生成试验记录
        /// </summary>
        /// <param name="record"></param>
        /// <returns></returns>
        public int SaveExperimentRecord(ExperimentRecord record)
        {
            return experimentRecordDBManager.Insert(record);
        }

        public void UpdateExperimentRecord(ExperimentRecord record)
        {
            experimentRecordDBManager.Update(record.Id, record);
        }

    }
}
